package com.navi.loaddata.job;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import com.navi.loaddata.loadService.impl.DefectData.LoadDefectData_AService;
import com.navi.loaddata.loadService.impl.baseData.LoadDefectGPRlnDataService;

public class LoadDefectData_A implements Job {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IEtlConfServices etlConfServices;

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private LoadDefectData_AService LoadDefectData_AService;

    @Autowired
    private LoadDefectGPRlnDataService LoadDefectGPRlnDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        EtlConfD etlConfD = (EtlConfD) jobDataMap.get("etlConf");
        etlConfD = etlConfServices.getNewConf(etlConfD);
        if(null==etlConfD) {
            logger.error("找不到该JOB。");
            return;
        }

        if ("N".equals(etlConfD.getRunFlg())) {
            etlConfD = etlConfServices.beginJob(etlConfD);
            if (StringUtils.isEmpty(etlConfD.getQueryMinute())) {
                return;
            }
            startETL(etlConfD);
        } else {
            logger.info("Job {}-{} is running.", etlConfD.getJobGroupName(), quartzManage.getRealJobName(etlConfD));
        }
    }
    private void startETL(EtlConfD etlConfD) {
        try {
            LoadDefectGPRlnDataService.loadDefectGPRlnDataMain(etlConfD);
            LoadDefectData_AService.loadDefectDataMain(etlConfD);
            etlConfServices.endJob(etlConfD);
            etlConfServices.addHis(etlConfD, null);
        } catch (ETLException e) {
            logger.info("Job {} [{}]-[{}] FAIL TO SYNCHRONIZE.", etlConfD.getProjectName() + etlConfD.getJobGroupName() + etlConfD.getJobName(), etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
            etlConfD.setCountSize(0);
            etlConfServices.endJob(etlConfD);
            etlConfServices.addHis(etlConfD,e.getMessage());

            if(null!=e.getMessage() && e.getMessage().contains("Unable to acquire JDBC Connection")){//无法连接,SLEEP
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setValidFlg("N");
                etlConfD.setRunFlg("N");
                etlConfServices.saveReloadJob(etlConfD);
                try{
                    Thread thread = Thread.currentThread();
                    thread.sleep(60000);//暂停60秒后程序继续执行
                }catch (InterruptedException ex) {
                    logger.error(LoggerUtil.stackTraceToString(ex));
                }
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setValidFlg("Y");
                etlConfServices.saveReloadJob(etlConfD);
                return;
            }
        }
    }
}
